package com.coder4j.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串处理工具类，生成随机数、格式校验、判断空字符串等
 * @author Keller
 * @date 2018-08-28 16:15:10
 */
public class StringUtils {

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 手机号校验规则：1开头的11位数字
     */
    private static final String PHONE_NUMBER_PATTERN = "^1+\\d{10}$";

    /**
     * 用户名校验规则：必须是字母开头，允许字母、数字、下划线
     */
    private static final String USER_NAME_PATTERN = "^[a-zA-Z][a-zA-Z0-9_]+$";

    /**
     * 邮箱校验规则：**@**.**
     */
    private static final String EMAIL_ADDRESS_PATTERN = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|" +
            "(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";


    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str == null || str.trim().isEmpty();
    }

    public static boolean notEmpty(String str){
        return !isEmpty(str);
    }

    /**
     * 将字符串的首字母转大写
     * @param str 需要转换的字符串
     * @return
     */
    public static String firstToUpper(String str) {
        if(isEmpty(str)){
            return null;
        }
        if(str.length() == 1){
            return str.toUpperCase();
        }
        return str.substring(0,1).toUpperCase() + str.substring(1,str.length());
    }

    public static String firstToLower(String str) {
        if(isEmpty(str)){
            return null;
        }
        if(str.length() == 1){
            return str.toLowerCase();
        }
        return str.substring(0,1).toLowerCase() + str.substring(1,str.length());
    }

    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return  sb.toString();
    }

    public static String lineToBigHump(String str) {
        str = lineToHump(str);
        String first = str.substring(0, 1).toUpperCase();
        return first + str.substring(1);
    }

    /**
     * 校验是否是用户名
     * @param string
     * @return
     */
    public static boolean isUserName(String string) {
        if(isEmpty(string)){
            return false;
        }
        Pattern pattern = Pattern.compile(USER_NAME_PATTERN);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();
    }

    /**
     * 校验是否是手机号
     * @param string
     * @return
     */
    public static boolean isPhoneNo(String string) {
        if(isEmpty(string)){
            return false;
        }
        Pattern pattern = Pattern.compile(PHONE_NUMBER_PATTERN);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();

    }

    /**
     * 校验是否是邮箱
     * @param string
     * @return
     */
    public static boolean isEmail(String string) {
        if(isEmpty(string)){
            return false;
        }
        Pattern pattern = Pattern.compile(EMAIL_ADDRESS_PATTERN);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();
    }

    public static void main(String[] args) {
        System.out.println("dd");
    }

}
